﻿/*-------------------------------------------------------------------------------------
													Client JS Logic used in album/xx.aspx
---------------------------------------------------------------------------------------*/
var AlbumPicList = function() {
	var _ajaxOpts = {
		type: 'post',
		dataType: 'json',
		contentType: 'application/json;charset=utf-8',
		cache: false
	};
	var togglePicDetailAdminMode = function() {
		if (AlbumPicList.AdminMode == "1") {
			$("#txtName").attr("readonly", false);
			$("#txtDesc").attr("readonly", false);
		} else {
			$("#txtName").attr("readonly", true);
			$("#txtDesc").attr("readonly", true);
			$("input,textarea", "#fileDetail").css({ border: "1px dotted #eee", color: "#7D982F" });
		}
	};
	return {
		InitPager: null,
		Pics: null,
		Asmx: null,
		AdminMode: null,
		AlbumID: null,
		AlbumTitle: null,
		SelectedIndex: null,
		Init: function(opts) {
			AlbumPicList.InitPager = opts.InitPagerStr;
			AlbumPicList.Asmx = opts.Asmx;
			AlbumPicList.AdminMode = opts.AdminMode || "0";
			AlbumPicList.AlbumID = opts.AlbumID;
			AlbumPicList.AlbumTitle = opts.AlbumTitle;
		}, //endof Init
		Select: function(opts) {
			var data = {};
			data.CPage = opts.CurrentPage;
			data.RPPage = opts.RowPerPage;
			data.AlbumID = AlbumPicList.AlbumID;
			var ajaxOpts = _ajaxOpts;
			ajaxOpts.url = AlbumPicList.Asmx + "SelectPics";
			ajaxOpts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					//Replace AlbumPicList
					$("#picsWrapper").html(msg.Context.piclisthtml);
					//Replace pager
					$("#pagerWrapper").html(msg.Context.pagerhtml);
					//Init pager again
					if (msg.Context.pager) {
						SimplePager.Init(msg.Context.pager, AlbumPicList.Select);
					}
					//Init jqGalView
					$("ul.jqGalView").jqGalView({
						title: AlbumPicList.AlbumTitle,
						openTxt: 'Hi,Open me',
						backTxt: '<<点击返回',
						goFullSizeTxt: '查看原图',
						imgContainerWidth: 116,
						imgContainerHeight: 85,
						zoomInCallback: AlbumPicList.ShowPicMetaData,
						zoomOutCallback: AlbumPicList.ShowPicMetaData
					});
					//Fetch Pics json data
					AlbumPicList.Pics = msg.Context.pics;
					//togglePicDetail edit mode
					togglePicDetailAdminMode();
				} else {
					alert("Something goes wrong!");
				}
			};
			ajaxOpts.data = $.toJSON({ Params: data });
			$.ajax(ajaxOpts);
		}, //endof Select
		UploadOkCallback: function(imgSrc) {
			//Save the img src to data base
			var data = {};
			data.AlbumID = AlbumPicList.AlbumID;
			data.ImgSrc = imgSrc;
			var ajaxOpts = _ajaxOpts;
			ajaxOpts.url = AlbumPicList.Asmx + "UploadPic";
			ajaxOpts.success = function(msg) {
				msg = msg.d;
				if (msg.IsOk) {
					AlbumPicList.Select($.evalJSON(AlbumPicList.InitPager));
				} else {
					alert("Something goes wrong!");
				};
			};
			ajaxOpts.data = $.toJSON({ Params: data });
			$.ajax(ajaxOpts);
		}, //endof uploadokcallback
		ShowPicMetaData: function(opts) {
			if (AlbumPicList.Pics == null || AlbumPicList.Pics.length < 1) {
				$("#fileDetail").hide();
				$("#fileDetailTip").show();
				return;
			};
			if (opts.index > -1) {
				$("#fileDetailTip").hide("fast", function() {
					$("#fileDetail").fadeIn();
				});

				var pic = AlbumPicList.Pics[opts.index];
				$("#txtDataKey").val(pic.Id);
				$("#txtName").val(pic.Name);
				$("#txtDesc").val(pic.Description);
				$("#txtUrl").val(pic.Url);
				AlbumPicList.SelectedIndex = opts.index;
			} else {
				$("#fileDetail").fadeOut("slow", function() {
					$("#fileDetailTip").show();
				});
				AlbumPicList.SelectedIndex = null;
			}
		}, //endof ShowPicMetaData
		SavePic: function(opts) {
			$("#btnSave").attr("value", "保存ing..").attr("disabled", true);
			//1,get data for saving
			var data = $(".datafield", "#fileDetail").serializeArray();
			//2.do save
			var ajaxopts = _ajaxOpts;
			ajaxopts.url = AlbumPicList.Asmx + "SavePic";
			ajaxopts.data = $.toJSON({ Params: data });
			ajaxopts.success = function(msg) {
				$("#btnSave").attr("value", "保 存").attr("disabled", false);
				msg = msg.d;
				if (msg.IsOk) {
					$("#statusbar").html("成功保存").show("fast", function() {
						var obj = $(this);
						window.setTimeout(function() {
							obj.fadeOut().html("");
						}, 1000);
					});
					//refresh data
					//AlbumPicList.Select($.evalJSON(AlbumPicList.InitPager));
					AlbumPicList.Pics[AlbumPicList.SelectedIndex].Name = msg.Context.data.Name;
					AlbumPicList.Pics[AlbumPicList.SelectedIndex].Description = msg.Context.data.Description;
				} else {
					alert(msg.Body);
				}
			};
			$.ajax(ajaxopts);
			ajaxopts = null;
		}, //endof SavePic
		DeletePic: function() {
			var id = $("#txtDataKey").val();
			var title = $("#txtName").val();
			if (!window.confirm("确认删除照片[" + title + "]?")) return;
			$("#btnDelete").attr("value", "删除ing..").attr("disabled", true);
			var data = {};
			data.id = id;
			var ajaxopts = _ajaxOpts;
			ajaxopts.url = AlbumPicList.Asmx + "DeletePic";
			ajaxopts.data = $.toJSON({ Params: data });
			ajaxopts.success = function(msg) {
				$("#btnDelete").attr("value", "删 除").attr("disabled", false);
				msg = msg.d;
				if (msg.IsOk) {
					//refresh data
					$("#statusbar").html("删除成功").show("fast", function() {
						var obj = $(this);
						window.setTimeout(function() {
							obj.html("").hide();
							$("#fileDetail").hide();
							$("#fileDetailTip").show();
							AlbumPicList.Select($.evalJSON(AlbumPicList.InitPager));
						}, 1000);
					});

				} else {
					alert(msg.Body);
				}
			};
			$.ajax(ajaxopts);
			ajaxopts = null;
		}, //endof DeletePic
		ClearPics: function(opts) {
			if (AlbumPicList.Pics == null || AlbumPicList.Pics.length < 1) {
				alert("相册里还没有任何照片");
				return;
			};
			if (!window.confirm("确认清空相册里的所有照片么?")) return;
			$("#btnClearPics").attr("value", "删除ing...").attr("disabled", true);
			var data = {};
			data.AlbumID = AlbumPicList.AlbumID;
			var ajaxopts = _ajaxOpts;
			ajaxopts.url = AlbumPicList.Asmx + "ClearPics";
			ajaxopts.data = $.toJSON({ Params: data });
			ajaxopts.success = function(msg) {
				$("#btnClearPics").attr("value", "清空相册").attr("disabled", false);
				msg = msg.d;
				if (msg.IsOk) {
					$("#picsWrapper").html("<h3>没有任何照片</h3>");
				} else {
					alert(msg.Body);
				};
			};
			$.ajax(ajaxopts);
			ajaxopts = null;
		} // /ClearPics
	};
} ();